//
//  StoreGoodsTableViewCell.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/19.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "StoreGoodsTableViewCell.h"
#import "ShopGoodsInforTableViewCell.h"
#import "StoreNameTableViewHeaderFooterView.h"
#import "NoDataFootTableViewHeaderFooterView.h"
//
#import "GoodsNumChangeView.h"

@interface StoreGoodsTableViewCell ()<UITableViewDataSource,UITableViewDelegate>{
    GLGWLabel * storeNameLabel;
    GLGWLabel * goodsNameLabel;
    GLGWLabel * specLabel;
    GLGWLabel * piceLabel;
    //
    GLGWButton * selectBut;
    GoodsNumChangeView * changeView;
}
@property(nonatomic,strong)MASConstraint *lable2TopConstraint;
@property (nonatomic,strong)UITableView * storeTableView;
@end

@implementation StoreGoodsTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        storeNameLabel = [[GLGWLabel alloc] initWithFont:SYSTEMFONT(12) theColor:kColor_Rule_Black inSuperView:self.contentView];
        [storeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(kWidthProportion_In_iPhoneTriploid(50));
            make.top.equalTo(self.contentView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(10));
            make.right.equalTo(self.contentView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(15));
        }];
        //
        goodsNameLabel = [[GLGWLabel alloc] initWithFont:SYSTEMFONT(14) theColor:kColor_Rule_Black inSuperView:self.contentView];
        [goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self->storeNameLabel);
            self.lable2TopConstraint = make.top.equalTo(self->storeNameLabel.mas_bottom).offset(kHeightProportion_In_iPhoneTriploid(10));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(20)));
        }];
        //
        specLabel = [[GLGWLabel alloc] initWithFont:SYSTEMFONT(12) theColor:[UIColor colorWithHexString:@"#666666"] inSuperView:self.contentView];
        [specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self->goodsNameLabel);
            make.top.equalTo(self->goodsNameLabel.mas_bottom).offset(kHeightProportion_In_iPhoneTriploid(10));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(17)));
        }];
        //
        changeView =[[GoodsNumChangeView alloc] initWithInSuperView:self.contentView];
        [changeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(15));
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-kHeightProportion_In_iPhoneTriploid(15));
            make.width.equalTo(@(kWidthProportion_In_iPhoneTriploid(70)));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(20)));
        }];
        kWeakSelf(self);
        changeView.Block_NumChange = ^(int gNum) {
            if (weakself.Block_GoodsNum) {
                weakself.Block_GoodsNum(gNum);
            }
        };
        //钱
        piceLabel = [[GLGWLabel alloc] initWithFont:SYSTEMFONT(16) theColor:[UIColor colorWithHexString:@"#EC5353"] inSuperView:self.contentView];
        [piceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self->specLabel);
            make.top.equalTo(self->specLabel.mas_bottom).offset(kHeightProportion_In_iPhoneTriploid(10));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(22)));
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-kHeightProportion_In_iPhoneTriploid(5));
        }];
        //
        selectBut = [[GLGWButton alloc] initInSuperView:self.contentView];
        selectBut.image_Name = @"icon_weixuanzhe";
        [selectBut setImage:[UIImage imageNamed:@"icon_xuanzhe_zhaopian"] forState:UIControlStateSelected];
        [selectBut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->goodsNameLabel.mas_top).offset(0);
            make.left.equalTo(self.contentView.mas_left).offset(0);
            make.width.equalTo(@(kWidthProportion_In_iPhoneTriploid(50)));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(50)));
        }];
        [selectBut addTarget:self action:@selector(goodsSelect:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:self.storeTableView];
    }
    return self;
}
-(void)goodsSelect:(GLGWButton *)sender{
//    _shopProductModel.isSelectBool = sender.selected;
    if (self.Block_GoodsSelect) {
        self.Block_GoodsSelect(sender);
    }
}
#pragma mark -- set
-(void)setShopProductModel:(ShopCarModel_Data *)shopProductModel{
    _shopProductModel = shopProductModel;
    if (_shopProductModel.isSimilar) {
        storeNameLabel.attributedText = [GLGWLabel addImageName:@"icon_dianpu_jinru" theImageFram:CGRectMake(0, -3, kWidthProportion_In_iPhoneTriploid(17), kHeightProportion_In_iPhoneTriploid(15)) theTextString:[NSString stringWithFormat:@"  %@",koString(_shopProductModel.shopName)] atImageIndex:0];
    }else{
        [self.lable2TopConstraint  uninstall];
        [goodsNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            self.lable2TopConstraint= make.top.equalTo(self.contentView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(10));
        }];
    }
    //
    goodsNameLabel.text = [NSString stringWithFormat:@"%@  %@  %@",koString(_shopProductModel.productName),koString(_shopProductModel.productMaterial),koString(_shopProductModel.factoryName)];
    specLabel.text = _shopProductModel.specifications;
    //
    piceLabel.attributedText = [GLGWLabel bllChangeStr:[NSString stringWithFormat:@"¥%.2f（%.2f吨）",_shopProductModel.price,_shopProductModel.singleWeight] changewithStr:[NSString stringWithFormat:@"（%.2f吨）",_shopProductModel.singleWeight] changeColoer:[UIColor colorWithHexString:@"#EC5353"] withFont:SYSTEMFONT(12)];
    //
    changeView.goodsNum = _shopProductModel.num;
    //
    selectBut.selected = _shopProductModel.isSelectBool;
    if (_shopProductModel.productQuantity == 0) {
        changeView.hidden = YES;
        storeNameLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        goodsNameLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        specLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        piceLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    }else{
        changeView.hidden = NO;
        storeNameLabel.textColor = kColor_Rule_Black;
        goodsNameLabel.textColor = kColor_Rule_Black;
        specLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        piceLabel.textColor = [UIColor colorWithHexString:@"#EC5353"];
    }
}

#pragma mark --UITableViewDataSource,UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kHeightProportion_In_iPhoneTriploid(95);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopGoodsInforTableViewCell * cell = [ShopGoodsInforTableViewCell cellWithTableView:tableView];
    if (indexPath.row == 1) {
        [cell setSepartionLineWith:lineModel_All];
    }else{
        [cell setSepartionLineWith:lineModel_Half];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    StoreNameTableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"StoreNameTableViewHeaderFooterView"];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kHeightProportion_In_iPhoneTriploid(25);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NoDataFootTableViewHeaderFooterView * footView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}


-(UITableView *)storeTableView{
    if (!_storeTableView) {
        _storeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(215)) style:UITableViewStyleGrouped];
        _storeTableView.delegate = self;
        _storeTableView.dataSource = self;
        _storeTableView.showsVerticalScrollIndicator = NO;
        _storeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _storeTableView.estimatedRowHeight = 0;
        _storeTableView.estimatedSectionHeaderHeight = 0;
        _storeTableView.estimatedSectionFooterHeight = 0;
        _storeTableView.scrollEnabled = NO;
        [_storeTableView registerClass:[StoreNameTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"StoreNameTableViewHeaderFooterView"];
        [_storeTableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    }
    return _storeTableView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
